//
//  BChargeCashChargeContentView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/7.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BChargeCashChargeContentView.h"
//
#import "BChargeCashChargeAddressView.h"

//
#import "UserWalletChargeResponse.h"


static CGFloat BChargeCashChargeContentViewButtonHeight = 35.0;
static CGFloat BChargeCashChargeContentViewDesPadding =20.0;

@interface BChargeCashChargeContentView ()

@property (nonatomic, strong) UIScrollView *contentScroll;

#pragma mark - views

/**
 *addressView
 */
@property (nonatomic, strong) BChargeCashChargeAddressView *addressView;

/**
 *descriptionLabel
 */
@property (nonatomic, strong) QMUILabel *descriptionLabel;

@end

@implementation BChargeCashChargeContentView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.contentScroll = [UIScrollView new];
    self.contentScroll.alwaysBounceVertical = true;
    self.contentScroll.showsVerticalScrollIndicator = false;
    [self addSubview:self.contentScroll];
    [self.contentScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    [self setupContents];
}
- (void)setupContents {
    self.addressView = [BChargeCashChargeAddressView new];
    [self.contentScroll addSubview:self.addressView];
    [self.addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentScroll).offset(0.0);
        make.centerX.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll);
        make.height.mas_equalTo(0);
    }];
    //
    CGFloat buttonPadding = 5.0f;
    CGFloat buttonWidth = ([BChargeCashChargeAddressView qrIVSize].width-buttonPadding)/2;
    self.addressCopyButton = [QMUIButton new];
    [self.addressCopyButton setTitleColor:[UIColor qmui_colorWithHexString:@"#999999"] forState:UIControlStateNormal];
    self.addressCopyButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.contentScroll addSubview:self.addressCopyButton];
    //
    self.saveQrButton = [QMUIButton new];
    [self.saveQrButton setTitleColor:[UIColor qmui_colorWithHexString:@"#999999"] forState:UIControlStateNormal];
    self.saveQrButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.contentScroll addSubview:self.saveQrButton];
    //
    [self.addressCopyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.addressView.mas_bottom);
        make.right.mas_equalTo(self.mas_centerX).offset(-buttonPadding/2);
        make.width.mas_equalTo(buttonWidth);
        make.height.mas_equalTo(BChargeCashChargeContentViewButtonHeight);
    }];
    //
    [self.saveQrButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.addressCopyButton.mas_right).offset(buttonPadding);
        make.top.mas_equalTo(self.addressCopyButton);
        make.width.mas_equalTo(buttonWidth);
        make.height.mas_equalTo(BChargeCashChargeContentViewButtonHeight);
    }];
    //
    self.addressCopyButton.layer.cornerRadius = self.saveQrButton.layer.cornerRadius = 3;
    self.addressCopyButton.layer.borderColor = self.saveQrButton.layer.borderColor = [UIColor qmui_colorWithHexString:@"#979797"].CGColor;
    self.addressCopyButton.layer.borderWidth = self.saveQrButton.layer.borderWidth = 1;
    //
    self.addressCopyButton.contentEdgeInsets = self.saveQrButton.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
    self.addressCopyButton.titleLabel.adjustsFontSizeToFitWidth = self.saveQrButton.titleLabel.adjustsFontSizeToFitWidth = true;
    //
    self.descriptionLabel = [QMUILabel new];
    self.descriptionLabel.numberOfLines = 0;
    self.descriptionLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.descriptionLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [self.contentScroll addSubview:self.descriptionLabel];
    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.addressCopyButton.mas_bottom).offset(BChargeCashChargeContentViewDesPadding);
        make.centerX.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll).offset(-40.0);
    }];
}

- (void)showContentScroll:(BOOL)show {
    self.contentScroll.alpha = show?1:0;
}






- (void)setChargeData:(UserWalletChargeData *)chargeData {
    _chargeData = chargeData;
    if (_chargeData) {
        self.contentScroll.contentOffset = CGPointZero;
        //
        CGFloat addressHeight = [self.addressView getAddressContentHeight:chargeData];
        [self.addressView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(addressHeight);
        }];
        //设置按钮
        [self.addressCopyButton setTitle:chargeData.flow.qrCopy_submit forState:UIControlStateNormal];
        [self.saveQrButton setTitle:chargeData.flow.save_submit forState:UIControlStateNormal];
        //
        if (chargeData.address.length) {
            self.addressCopyButton.enabled = self.saveQrButton.enabled = true;
        }else {
            self.addressCopyButton.enabled = self.saveQrButton.enabled = false;
        }
        
        //
        self.descriptionLabel.text = chargeData.flow.desc;
        //
        [self.contentScroll layoutIfNeeded];
        //
        CGFloat contentHeight = 0;
        contentHeight += addressHeight;
        contentHeight += BChargeCashChargeContentViewButtonHeight;
        contentHeight += BChargeCashChargeContentViewDesPadding;
        contentHeight += CGRectGetHeight(self.descriptionLabel.frame);
        //
        self.contentScroll.contentSize = CGSizeMake(0, contentHeight);
    }
}




- (UIImage *)qrImage {
    return self.addressView.qrImage;
}


@end
